package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/system/service"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"boy-go/pkg/response"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"strings"
)

// AddSysOperLogRouter 添加路由
func AddSysOperLogRouter(group *gin.RouterGroup) {
	group.GET("/operlog/:id", middlewares.HasPermission("monitor:operlog:query"), get_operlog)
	group.GET("/operlog/list", middlewares.HasPermission("monitor:operlog:list"), list_operlog)
	group.POST("/operlog", middlewares.HasPermission("monitor:operlog:add"), middlewares.Logger("操作日志记录", middlewares.INSERT), add_operlog)
	group.PUT("/operlog", middlewares.HasPermission("monitor:operlog:edit"), middlewares.Logger("操作日志记录", middlewares.UPDATE), edit_operlog)
	group.DELETE("/operlog/:ids", middlewares.HasPermission("monitor:operlog:remove"), middlewares.Logger("操作日志记录", middlewares.DELETE), del_operlog)
	group.DELETE("/operlog/clean", middlewares.HasPermission("monitor:operlog:remove"), middlewares.Logger("操作日志记录", middlewares.CLEAN), clean_operlog)
}

// get_operlog
// @Summary 获取操作日志记录详情
// @Description 获取操作日志记录详情（管理页面）
// @Tags monitor-operlog
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.SysOperLogModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /monitor/operlog/{id} [get]
func get_operlog(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewSysOperLogService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_operlog
// @Summary 获取操作日志记录列表
// @Description 获取操作日志记录列表（管理页面）
// @Tags monitor-operlog
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.SysOperLogModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /monitor/operlog/list [get]
func list_operlog(ctx *gin.Context) {
	req := vo.QuerySysOperLogReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	page, err := service.NewSysOperLogService().List(&req)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxDataTable(ctx, page.List, page.Total)
}

// add_operlog
// @Summary 新增操作日志记录
// @Description 新增操作日志记录（管理页面）
// @Tags monitor-operlog
// @Accept json
// @Produce json
// @Param req body  vo.AddSysOperLogReq true "req"
// @Success 200 {object} response.R[vo.AddSysOperLogRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /monitor/operlog [post]
func add_operlog(ctx *gin.Context) {
	var req vo.AddSysOperLogReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysOperLogService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_operlog
// @Summary 编辑操作日志记录
// @Description 编辑操作日志记录（管理页面）
// @Tags monitor-operlog
// @Accept json
// @Produce json
// @Param req body  vo.EditSysOperLogReq true "req"
// @Success 200 {object} response.R[vo.EditSysOperLogRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /monitor/operlog [put]
func edit_operlog(ctx *gin.Context) {
	var req vo.EditSysOperLogReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysOperLogService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_operlog
// @Summary 删除操作日志记录
// @Description 删除操作日志记录（管理页面）
// @Tags monitor-operlog
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /monitor/operlog [delete]
func del_operlog(ctx *gin.Context) {
	var idsStr = ctx.Param("ids")
	ids := strings.Split(idsStr, ",")
	err := service.NewSysOperLogService().BatchDel(ids)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, nil)
	}
}

func clean_operlog(ctx *gin.Context) {
	err := service.NewSysOperLogService().Clean()
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, nil)
}
